﻿using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Logistics;
using Nop.Core.Domain.Shipping;
using Nop.Core.Plugins;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Logistics;
using Nop.Services.Shipping;
using Nop.Services.Shipping.Tracking;
using System;
using System.Web.Routing;

namespace Spotshub.Plugin.Shipping.Spotshub
{
    public class SpotshubComputationMethod : BasePlugin, IShippingRateComputationMethod
    {
        #region Fields

        private readonly ISettingService _settingService;
        private readonly IShippingService _shippingService;
        private readonly ILogisticsService _logisticsService;
        private readonly ILocalizationService _localizationService;
        private readonly IWorkContext _workContext;
        private readonly ILogger _logger;

        #endregion

        #region Ctor
        public SpotshubComputationMethod(ISettingService settingService,
            IShippingService shippingService,
            ILogisticsService logisticsService,
            ILocalizationService localizationService,
            IWorkContext workContext,
            ILogger logger)
        {
            this._settingService = settingService;
            this._shippingService = shippingService;
            this._logisticsService = logisticsService;
            this._localizationService = localizationService;
            this._workContext = workContext;
            this._logger = logger;
        }
        #endregion

        public bool ValidateDimensionAndWeight(PackageInformation pi)
        {
            //improve when this have been used
            return true;
        }

        public ShippingRateComputationMethodType ShippingRateComputationMethodType
        {
            get { return ShippingRateComputationMethodType.Offline; }
        }

        public GetShippingOptionResponse GetShippingOptions(GetShippingOptionRequest getShippingOptionRequest)
        {
            if (getShippingOptionRequest == null)
                throw new ArgumentNullException("getShippingOptionRequest");

            var response = new GetShippingOptionResponse();

            if (getShippingOptionRequest.Items == null || getShippingOptionRequest.Items.Count == 0)
            {
                response.AddError("No shipment items");
                return response;
            }


            Region toRegion = _logisticsService.GetRegionByZipPostalCode(getShippingOptionRequest.ShippingAddress.ZipPostalCode);
            if (toRegion != null)
            {
                var shippingMethods = _shippingService.GetAllShippingMethods(getShippingOptionRequest.ShippingAddress.CountryId);
                foreach (var shippingMethod in shippingMethods)
                {
                    var shippingOption = new ShippingOption();
                    shippingOption.Name = shippingMethod.GetLocalized(x => x.Name);
                    shippingOption.Description = shippingMethod.GetLocalized(x => x.Description);
                    shippingOption.Rate = 0M;
                    response.ShippingOptions.Add(shippingOption);
                }

                //Region fromRegion = _logisticsService.GetRegionByZipPostalCode(getShippingOptionRequest.ZipPostalCodeFrom);
                //if (fromRegion != null && fromRegion.Id == toRegion.Id)
                //{
                //    var shippingOption = new ShippingOption();
                //    shippingOption.Name = this.GetLocalizedFriendlyName(_localizationService, _workContext.WorkingLanguage.Id);
                //    shippingOption.Description = _localizationService.GetResource("Plugins.Shipping.Spotshub.Description");
                //    shippingOption.Rate = 0M;
                //    response.ShippingOptions.Add(shippingOption);
                //}
                //else if (fromRegion == null)
                //{
                //    response.AddError(String.Format("The zip/postal code ({0}) is not supported.", getShippingOptionRequest.ZipPostalCodeFrom));
                //}
            }
            return response;
        }

        public decimal? GetFixedRate(GetShippingOptionRequest getShippingOptionRequest)
        {
            if (getShippingOptionRequest == null)
                throw new ArgumentNullException("getShippingOptionRequest");

            return null;
        }

        public IShipmentTracker ShipmentTracker
        {
            get { return new SpotshubShipmentTracker(this._logger, this._logisticsService); }
        }

        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "ShippingSpotshub";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Spotshub.Plugin.Shipping.Spotshub.Controllers" }, { "area", null } };
        }

        public override void Install()
        {
            this.AddOrUpdatePluginLocaleResource("Plugins.Shipping.Spotshub.Description", "SpotsHUB EXPRESS Shipping Service");
            base.Install();
        }

        public override void Uninstall()
        {
            this.DeletePluginLocaleResource("Plugins.Shipping.Spotshub.Description");
            base.Uninstall();
        }
    }
}
